<?php

class Jf_View_Block_Tree_Breadcrumbs extends Jf_View_Block_Tree_Abstract {

    protected $_separator = ' &gt; ';

    protected $_minDepth;

    protected $_linkLast = false;

    protected $_partial;

    public function breadcrumbs (Jf_Tree_Container $container = null, $options = null) {
        if (null !== $container) {
            $this -> setContainer ($container);
        }
        if (null !== $options) {
            foreach ($options as $key => $value) {
                $method = 'set' . ucfirst ($key);
                $this -> $method ($value);
            }
        }
        return $this;
    }

    public function setSeparator ($separator) {
        if (is_string ($separator)) {
            $this -> _separator = $separator;
        }
        return $this;
    }

    public function getSeparator () {
        return $this -> _separator;
    }

    public function setLinkLast ($linkLast) {
        $this -> _linkLast = (bool) $linkLast;
        return $this;
    }

    public function getLinkLast () {
        return $this -> _linkLast;
    }

    public function setPartial ($partial) {
        if (null === $partial || is_string ($partial) || is_array ($partial)) {
            $this -> _partial = $partial;
        }
        return $this;
    }

    public function renderStraight (Zend_Navigation_Container $container = null) {
        if (null === $container) {
            $container = $this -> getContainer ();
        }
        // find deepest active
        if ( ! $active = $this -> findActive ($container)) {
            return '';
        }
        $active = $active['node'];
        // put the deepest active node last in breadcrumbs
        if ($this -> getLinkLast ()) {
            $html = $this -> htmlify ($active);
        } else {
            if ($active -> getParent () === $container) {
                $html = '<strong>' . $active -> getLabel () . '</strong>';
            } else {
                $html = $active -> getLabel ();
            }
        }
        // walk back to root
        while ($parent = $active -> getParent ()) {
            // if ($parent instanceof Jf_Tree_Node) {
            if ($parent instanceof Jf_Tree_Node && $parent -> getHref ()) {
                $html = $this -> htmlify ($parent) . $this -> getSeparator () . $html;
            }
            if ($parent === $container) {
                break;
            }
            $active = $parent;
        }
        return strlen ($html) ? $this -> getIndent () . $html : '';
    }

    public function render () {
        return $this -> renderStraight ();
    }

}
